/*
 * Copyright [2011] [Henry and Will]

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

     http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
 * */
package org.hnw.wte;

import java.util.ArrayList;

import android.R;
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

public class listview extends ListActivity{
	
	private wteDataBase database;
	private ArrayList<Restaurant> Rest_list;
	private ArrayList<String> listV = new ArrayList<String>();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
//		Toast.makeText(this, "You enter the listview!!!", Toast.LENGTH_SHORT).show();
		database = new wteDataBase(this);
		database.DBopen();

		//following sentences are meant to insert some tempory data for testing
//		Restaurant temp = new Restaurant("McDownload", "Cheap and Disgusting!!!", "Garfield & Garvey", "1.png");
//		database.DBinsert(temp);
		
		
		//the following sentence can clean the table
//		wteCleanTable();
		
		saveItemOnList();
		setListAdapter(new ArrayAdapter<String>(this, org.hnw.wte.R.layout.listview, listV));
		
		ListView lv = getListView();
		lv.setTextFilterEnabled(true);
		
		//define the action when click the item in the list
		lv.setOnItemClickListener(new OnItemClickListener(){

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				// TODO Auto-generated method stub
				Toast.makeText(getApplicationContext(), listV.get(arg2), Toast.LENGTH_LONG).show();
			}
			
		});
		
	}
	
	
	//define the menu button under this activity
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// TODO Auto-generated method stub
		super.onCreateOptionsMenu(menu);
		MenuInflater myinf = getMenuInflater();
		myinf.inflate(org.hnw.wte.R.menu.listview_menu, menu);
		return true;
	}


	//define the function of the item of menu button
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		this.startActivity(new Intent(this, AddRestActivity.class));
//		Toast.makeText(this, "You selected Add Restaurant!!!", Toast.LENGTH_SHORT).show();
		return true;
	}



	//save all cursor items in a string list which will show on the activity
	private void saveItemOnList(){
		//fetch all data from the table
		Rest_list = database.DBretrieveAll();
		
		if(Rest_list.isEmpty()){
			database.DBclose();
			Toast.makeText(this, "There is no restaurant to show!!!", Toast.LENGTH_SHORT).show();
//			startActivity(new Intent("android.intent.action.MAIN"));
		}else{
			//add every item from the query to list view
			for(Restaurant temp:Rest_list){
				listV.add(temp.getName());
			}
		}
			
	}
	
	//delete all datas from the table
	private void wteCleanTable(){
		for(Restaurant temp:Rest_list)
			database.DBremove(temp.getID());
	}
	
	protected void onResume() {
		super.onResume();
	}
	
	protected void onPause() {
		super.onPause();
	}
	
}
